package com.xishu.controller;

import com.xishu.annotation.valid.MethodValidator;
import com.xishu.config.Constant;
import com.xishu.entity.comment.Comment;
import com.xishu.entity.customer.Customer;
import com.xishu.response.ResponseData;
import com.xishu.response.ResponseStatus;
import com.xishu.response.VerifyUtil;
import com.xishu.service.CommonService;
import com.xishu.service.CustomerService;
import com.xishu.util.ServletUtil;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class CommentController {

    private CommonService commonService = CommonService.getInstance();

    private CustomerService customerService = CustomerService.getInstance();

    @ApiModelProperty(value = "发表评论")
    @PostMapping(value = ("/Comment/add"), produces = ("application/json;charset=UTF-8"))
    @MethodValidator(mustLogin = false, requireCustomerLogin = true)
    public ResponseData add(@RequestBody Comment comment) throws Exception {
        VerifyUtil.isEmpty(comment.getShopId(), comment.getParentCommentId(),comment.getContent());
        Long customerId = ServletUtil.getSessionAttribute(Constant.SESSION_CUSTOMER);
        Customer customer = customerService.findCustomer(customerId);
        comment.setHeadImg(customer.getHeadImg());
        comment.setCustomerName(customer.getName());
        comment.setCustomerId(customerId);
        //  菜品id为空，则代表发表的是商店评论 评论类型 0-商店评论 1-菜品评论
        if(comment.getProductId()==null){
            comment.setType(0);
        }else {
            comment.setType(1);
        }
        return commonService.createObject(comment, null, true, ResponseStatus.NAME_EXISTS);
    }

    @ApiOperation(value = "删除评论")
    @DeleteMapping(value = ("/Comment/delete"), produces = ("application/json;charset=UTF-8"))
    public ResponseData delete(@RequestBody Comment comment) throws Exception {
        VerifyUtil.verify(()->comment.getId()!=null);
        return commonService.delete(comment);
    }

    @ApiOperation(value = "查询评论")
    @PutMapping(value = ("/Comment/search"), produces = ("application/json;charset=UTF-8"))
    public ResponseData search(@RequestBody Comment comment) throws Exception {
        VerifyUtil.verify(()->comment.getShopId()!=null);
        // 如果没传菜单品id 则查商店的评论
        List<Comment> list=null;
        if(comment.getProductId()==null){
            comment.setType(0);
            list = getComments(comment);
        }else {
            comment.setType(1);
            list = getComments(comment);
        }
        return commonService.createResponse(list);
    }

    private List<Comment> getComments(Comment comment) {
        List<Comment> comments = commonService.searchList(comment);
        //查询出所有一级评论
        List<Comment> collect = comments.stream().filter((c) -> {
            return c.getParentCommentId() == 0;
        }).collect(Collectors.toList());
        //设置子评论
        List<Comment> list = collect.stream().map((c) -> {
             c.setChildren(getChidren(c, comments));
             return c;
        }).sorted((o1, o2) -> {
            return   -(o1.getCreateTime().compareTo(o2.getCreateTime()));
        }).collect(Collectors.toList());
        return list;
    }

    private List<Comment> getChidren(Comment root, List<Comment> Comments) {
        List<Comment> collect = Comments.stream().filter(comment -> {
            return comment.getParentCommentId().equals(root.getId());
        }).sorted((o1, o2) -> {
          return   -(o1.getCreateTime().compareTo(o2.getCreateTime()));
        }).collect(Collectors.toList());

        return collect;
    }
}
